package com.atguigu.lease.web.admin.test;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;

/**
 * @author 张英杰
 * @date 2025/10/14/014 19:20
 */
public class T2 {
    // 服务器service_key
    static String service_key = "atguigu1231232131231231242141241241241414";

    public static void main(String[] args) {

        String token = "eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFMzODQifQ.eyJpZCI6MSwibmFtZSI6IuW8oDMifQ.GOAbXaW9hU4f64rAN1Ztb2VhgUn8TTHZIOLBV3clNZ-PbrUmUVRref1qOxo3MQA9";

        // 环境
        String ip = "127.0.0.1";

        // 解析
        String salt = ip + service_key;
        SecretKey secretKey = Keys.hmacShaKeyFor(salt.getBytes());

        JwtParser parser = Jwts.parserBuilder().setSigningKey(secretKey).build();

        Jws<Claims> claimsJws = parser.parseClaimsJws(token);
        System.out.println(claimsJws);

    }

    public static String createToken(){


        // 用户信息
        User user = new User();
        user.setId(1L);
        user.setName("张3");
        user.setAge(20);

        // 环境
        String ip = "127.0.0.1";

        // 签名盐
        String salt = ip + service_key;
        SecretKey secretKey = Keys.hmacShaKeyFor(salt.getBytes());

        String token = Jwts.builder()
                .setHeaderParam("type","JWT")
                .setHeaderParam("alg","HS256")
                .claim("id",user.getId())
                .claim("name",user.getName())
                .signWith(secretKey)
                .compact();

        System.out.println(token);

        return token;
    }

}
